package com.shujia.dao;

import com.shujia.bean.Student;
import com.shujia.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class StudengDaoImpl implements StudentDao{

    /**
     * 通过id查询学生信息的方法
     * @param id
     * @return
     */
    @Override
    public Student queryStudentById(String id) {

        Student student = null;

        try {
            //1、获取jdbc连接
            Connection con = JDBCUtil.getCon();

            //2、通过id查询学生信息
            PreparedStatement ps = con.prepareStatement("select * from student where id=?");

            //3、设置id参数
            ps.setString(1, id);

            //4、执行sql
            ResultSet rs = ps.executeQuery();

            //5、解析数据
            while (rs.next()) {
                String name = rs.getString("name");
                Integer age = rs.getInt("age");
                String gender = rs.getString("gender");
                String clazz = rs.getString("clazz");

                student = new Student(id, name, age, gender, clazz);
            }

            //6、关闭连接
            con.close();

        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        //7、返回结果
        return student;
    }
}
